package hong.jun.pet_home.service.order.impl;

import hong.jun.basic.base.service.impl.BaseServiceImpl;
import hong.jun.basic.util.CodeGenerateUtils;
import hong.jun.basic.util.DistanceUtil;
import hong.jun.pet_home.domain.order.OrderAddress;
import hong.jun.pet_home.domain.order.ProductOrder;
import hong.jun.pet_home.domain.order.ProductOrderDetail;
import hong.jun.pet_home.domain.org.Shop;
import hong.jun.pet_home.domain.pay.PayBill;
import hong.jun.pet_home.domain.product.Product;
import hong.jun.pet_home.domain.user.User;
import hong.jun.pet_home.domain.user.UserAddress;
import hong.jun.pet_home.domain.util.SmsConstants;
import hong.jun.pet_home.mapper.*;
import hong.jun.pet_home.service.order.IProductOrderService;
import hong.jun.pet_home.service.pay.IPayBillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class ProductOrderServiceImpl extends BaseServiceImpl<ProductOrder> implements IProductOrderService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private ProductOrderDetailMapper productOrderDetailMapper;
    @Autowired
    private IPayBillService payBillService;

    /**
     * 服务购买
     * 储存服务订单表 关联储存订单地址表 ，订单商品详情表
     *
     * @param params
     */
    @Override
    public String payproduct(Map<String, Object> params, User user) {
        /* address_id: "1"
        pay_method: "2"
        product_id: 175
        product_num: "3"
        service_method: "0"*/
        System.out.println("user======" + user);
        Long addressId = Long.parseLong(params.get("address_id").toString());  //收件地址id
        Long productId = Long.parseLong(params.get("product_id").toString());//商品id
        Long productNum = Long.parseLong(params.get("product_num").toString());//购买数量
        Long pay_method = Long.parseLong(params.get("pay_method").toString());//支付方式  0 余额 1 支付宝 2 微信 3 银联
        //工具类创建统一支付单号
        String unionPaySn = CodeGenerateUtils.generateUnionPaySn();
        //根据id查询商品
        Product product = productMapper.loadById(productId);
        //根据id查询服务地址详情转换为商品地址详情
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        //详情转换为订单商品详情
        ProductOrderDetail productOrderDetail = Product_ProductOrderDetail(product);
        //服务地址详情转换为商品地址详情
        OrderAddress orderAddress = userAddress_orderAddress(userAddress);
        //处理商品服务订单表
        ProductOrder productOrder = newProductOrder(productNum, userAddress, product, user, unionPaySn);
        //保存商品服务订单表
        productOrderMapper.save(productOrder);
        //保存订单地址表
        orderAddress.setOrder_id(productOrder.getId());
        orderAddress.setOrderSn(productOrder.getOrderSn());
        orderAddressMapper.save(orderAddress);
        //保存订单商品详情表
        productOrderDetail.setOrder_id(productOrder.getId());
        productOrderDetailMapper.save(productOrderDetail);
        //创建支付单
        PayBill payBill = newPayBill(productOrder, product, user, pay_method, unionPaySn);
        //保存支付单
        //payBillService.save(payBill);
        //跳转到支付页面,通过调用支付接口，要把这个数据返回前台，前台处理后跳转支付宝支付页面
        String payData = payBillService.pay(payBill);
        System.out.println(payData);
        return payData;
    }

    /**
     * 创建支付单
     */
    public PayBill newPayBill(ProductOrder productOrder, Product product, User user, Long pay_method, String unionPaySn) {
        PayBill payBill = new PayBill();
        payBill.setDigest(productOrder.getDigest()); //交易摘要
        payBill.setMoney(productOrder.getPrice()); //BigDecimal money;       //交易金额
        payBill.setUnionPaySn(unionPaySn);   //统一支付单号
        payBill.setState(0);//Integer state;          //支付状态 0待支付 1已支付 -1取消
        payBill.setLastPayTime(productOrder.getLastPayTime());//  最后支付时间
        payBill.setPayChannel(pay_method);// //支付方式  0 余额 1 支付宝 2 微信 3 银联
        payBill.setBusinessType(SmsConstants.BUSINESSTYPE_PRODUCT);// 业务类型商品服务
        payBill.setBusinessKey(product.getId());// 商品id
        payBill.setCreateTime(new Date());//创建时间
        payBill.setUser_id(user.getId()); //购买用户id
        payBill.setNickName(user.getUsername());//用户账号
        payBill.setShop_id(productOrder.getShop_id()); //提供服务的商铺id
        return payBill;
    }

    //服务地址详情转换为商品地址详情
    public OrderAddress userAddress_orderAddress(UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress, orderAddress);
        // System.out.println("orderAddress====="+orderAddress);
        return orderAddress;
    }

    //商品详情转换为订单商品详情
    public ProductOrderDetail Product_ProductOrderDetail(Product product) {
        ProductOrderDetail productOrderDetail = new ProductOrderDetail();
        BeanUtils.copyProperties(product, productOrderDetail);
        //System.out.println("productOrderDetail====="+productOrderDetail);
        return productOrderDetail;
    }

    //创建订单表 处理数据
    public ProductOrder newProductOrder(Long productNum, UserAddress userAddress, Product product, User user, String unionPaySn) {
        ProductOrder productOrder = new ProductOrder();
        productOrder.setDigest("[宠物服务订单]购买" + productNum + "次" + product.getName() + "服务！");//摘要
        productOrder.setState(0);       //默认未支付
        BigDecimal price = product.getSaleprice().multiply(new BigDecimal(productNum));//计算价格
        productOrder.setPrice(price);//价格
        productOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));//同一订单编号
        productOrder.setPaySn(unionPaySn);//同一支付单号
        Date lastPayTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000); //最迟付款时间当前时间+30分钟
        productOrder.setLastPayTime(lastPayTime);//最迟支付时间
        productOrder.setProduct_id(product.getId());                        //商品id
        productOrder.setUser_id(user.getId());                              //购买用户id
        Shop nearestShop = DistanceUtil.getNearestShop(DistanceUtil.getPoint(userAddress.getFullAddress()), shopMapper.loadAll());  //分配店铺
        productOrder.setShop_id(nearestShop.getId());//商铺id
        return productOrder;
    }
}
